package hn.cch.version.lts11;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.jupiter.api.Test;

public class NewFeatureTest {


    /**
     * Arrays.asList返回java.util.List
     */
    @Test
    public void testArray() {
        Arrays.asList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9").forEach(System.out::println);
        // Arrays.asList(...)
        // List.of(...)
    }

    /**
     * 类String方法
     */
    @Test
    public void testString() {

        String string = "";


        // 判断字符串是否为空白
        string = " ";
        System.out.println(string.isBlank()); // true
        // 去除首尾空格
        string = " string strip ";
        System.out.println(string.strip()); // "string strip"
        // 去除尾部空格
        string = " string strip ";
        System.out.println(string.stripTrailing()); // " string strip"
        // 去除首部空格
        string = " string strip ";
        System.out.println(string.stripLeading()); // "string strip "
        // 复制字符串
        string = "Java";
        System.out.println(string.repeat(3));// "JavaJavaJava"
        // 行数统计
        string = "A\nB\nC";
        System.out.println(string.lines().count()); // 3

    }

    @Test
    public void testCopy() {

        List<String> list = List.of("Java", "Python", "C");
        List<String> copy = List.copyOf(list);
        System.out.println(list == copy); // true

        List<String> arrayList = new ArrayList<String>(list);
        List<String> arrayCopy = List.copyOf(list);
        System.out.println(arrayList == arrayCopy); // false


    }

    @Test
    public void testStream() {

        long count = Stream.ofNullable(null).count();// 0

        List<Integer> takeWhile = Stream.of(1, 2, 3, 2, 1).takeWhile(n -> n < 3).collect(Collectors.toList());// [1, 2]
        System.out.println(takeWhile);

        List<Integer> dropWhile = Stream.of(1, 2, 3, 2, 1).dropWhile(n -> n < 3).collect(Collectors.toList());// [3, 2, 1]
        System.out.println(dropWhile);


    }

    @Test
    public void testClassLoader() throws Exception {

        ClassLoader classLoader = ClassLoader.getSystemClassLoader();
        try (InputStream inputStream = classLoader.getResourceAsStream("file-name.txt")) {
            File tempFile = File.createTempFile("file-name", "txt");
            try (FileOutputStream outputStream = new FileOutputStream(tempFile)) {
                assert inputStream != null;
                inputStream.transferTo(outputStream);
            }
        }


    }


    @Test
    public void testFile() throws Exception {
        Files.writeString(Path.of("./", "tmp.txt"), // 路径
                "hello, jdk11 files api", // 内容
                StandardCharsets.UTF_8); // 编码
        String s = Files.readString(Paths.get("./tmp.txt"), // 路径
                StandardCharsets.UTF_8); // 编码
    }




}